package a07_二叉树;

/**
 * 最底层 最左边
 * 如何判断是最底层
 * 方法：用一个变量记录最大深度 一个变量记录结果
 *
 *
 * @author flyduck
 * @since 2025/1/2
 */
public class a16_找树左下角的值复习0 {
    public int findBottomLeftValue(TreeNode root) {
        traversal(root, 1);
        return result;
    }

    private Integer maxDepth = Integer.MIN_VALUE;
    private Integer result = 0;

    private void traversal(TreeNode currentNode,int currentDepth){
        if (currentNode == null) {
            return;
        }
        if (currentNode.left == null && currentNode.right == null) {
            if(currentDepth > maxDepth){
                maxDepth = currentDepth;
                result = currentNode.val;
            }
        }
        traversal(currentNode.left, currentDepth + 1);
        traversal(currentNode.right, currentDepth + 1);
    }
}
